package com.kgis.map.control {
   import flash.display.DisplayObject;
   import flash.display.Sprite;
   import flash.utils.getQualifiedClassName;
   import com.kgis.map.Map;
   import com.kgis.map.geometry.basetype.Pixel;
   import com.kgis.map.events.MapEvent;
   import com.kgis.map.control.IControl;

   /**
    * 地图上的控件基类
    */
   public class Control extends Sprite implements IControl {

      protected var _map:Map = null;
      protected var _active:Boolean = false;

      public function Control(position:Pixel = null) {

         if (position != null)
            this.position = position
         else
            this.position = new Pixel(0, 0);

         this.name = getQualifiedClassName(this).split('::')[1];
      }

      public function destroy():void {
         if (this.map != null)
            this.map.removeEventListener(MapEvent.RESIZE, this.draw);

         this.map = null;
      }

      public function get map():Map {
         return this._map;
      }

      public function set map(value:Map):void {
         if (this._map != null)
            this._map.removeEventListener(MapEvent.RESIZE, this.resize);
         this._map = value;

         this._map.addEventListener(MapEvent.RESIZE, this.resize);
      }

      public function resize(event:MapEvent):void {
         this.draw();
      }

      public function get active():Boolean {
         return this._active;
      }

      public function set active(value:Boolean):void {
         this._active = value;
      }

      public function set visiable(value:Boolean):void {
         this.visible = value;
      }

      public function draw():void {
         // Reset before drawing
         this.graphics.clear();
         var i:int = this.numChildren - 1;
         for (i; i > -1; i--) {
            removeChildAt(i);
         }
      }

      public function set position(px:Pixel):void {
         if (px != null) {
            this.x = px.x;
            this.y = px.y;
         }
      }

      public function get position():Pixel {
         return new Pixel(this.x, this.y);
      }

   }
}

